package com.mscsoft.tools;

import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;

import com.mscsoft.App;
import com.mscsoft.musicscore.MusicScore;

public class ResTools {
    /** 从本地资源文件夹载入谱面 */
    public static List<MusicScore> loadMusicScore() {
        ArrayList<MusicScore> musicScores = new ArrayList<>();
        try {
            String[] fileNames = new String(App.class.getResourceAsStream("/musicscore/").readAllBytes()).split("\n");
            for (String fileName : fileNames) {
                InputStream fInputStream = App.class.getResourceAsStream("/musicscore/" + fileName);
                MusicScore ms = MusicScore.Tools.readFromInputStream(fInputStream);
                if (ms != null)
                    musicScores.add(ms);
            }
        } catch (IOException e) {
            //
        }
        return musicScores;
    }

    /** 按歌曲ID升序排序 */
    public static void sortByID(List<MusicScore> list) {
        list.sort((e1, e2) -> e1.meta.id - e2.meta.id);
    }

    /** 按歌曲名称字典序排序 */
    public static void sortByName(List<MusicScore> list) {
        list.sort((e1, e2) -> e1.meta.music.title.compareTo(e2.meta.music.title));
    }

    /** 按歌曲难度升序排序 */
    public static void sortByDifficulty(List<MusicScore> list) {
        list.sort((e1, e2) -> e1.meta.level - e2.meta.level);
    }

    /** 按歌曲bpm升序排序 */
    public static void sortByBPM(List<MusicScore> list) {
        list.sort((e1, e2) -> Double.compare(e1.bpm, e2.bpm));
    }

    public static void main(String[] args) {
        List<MusicScore> list = loadMusicScore();
        System.out.println("原始排序:");
        list.forEach(System.out::println);
        System.out.println("\n按ID排序:");
        sortByID(list);
        list.forEach(System.out::println);
        System.out.println("\n按歌曲名字排序:");
        sortByName(list);
        list.forEach(System.out::println);
        System.out.println("\n按难度排序:");
        sortByDifficulty(list);
        list.forEach(System.out::println);
        System.out.println("\n按BPM排序:");
        sortByBPM(list);
        list.forEach(System.out::println);
    }
}
